/*
 * imx8-sig.c
 *
 *  Created on: 2024年3月13日
 *      Author: NI
 */

#include <pthread.h>
#include <signal.h>
#include <stdio.h>

volatile int aborted = 0;

static const int exceptsigs[] = {
	SIGCHLD,
	SIGIO,
	SIGURG,
	SIGWINCH,
	SIGSTOP,
	SIGTSTP,
	SIGTTIN,
	SIGTTOU,
	SIGCONT,
	-1,
};

static void *signal_thread(void *arg)
{
	sigset_t sigset;
	int sig;
	int i;

	pthread_setname_np(pthread_self(), "signal_thread");
	pthread_detach(pthread_self());
	sigfillset(&sigset);
	pthread_sigmask(SIG_BLOCK, &sigset, NULL);

	for (i = 0; exceptsigs[i] != -1; i++)
	{
		sigdelset(&sigset, exceptsigs[i]);
	}

	for (;;)
	{
		if (sigwait(&sigset, &sig) == 0)
		{
			aborted = 1;
			break;
		}
	}
	return NULL;
}

int init_sig(){
	int i = 0,ret = -1;
	sigset_t sigset;
	sigfillset(&sigset);
	for (i = 0; exceptsigs[i] != -1; i++)
	{
		sigdelset(&sigset, exceptsigs[i]);
	}
	pthread_sigmask(SIG_BLOCK, &sigset, NULL);

	ret = pthread_create(NULL, NULL, signal_thread, NULL);
	if (ret)
	{
		printf("pthread_create failed\n");
		return ret;
	}

	return 0;
}
